%% Author: xianrongMai
%% Created: 2011-7-1
%% Description: 成就系统的组包和解包
-module(pt_38).
%%
%% Include files
%%
-include("common.hrl").
-include("achieve.hrl").
-include("table_to_record.hrl").
%%
%% Exported Functions
%%
-export([read/2, write/2]).

%%
%% API Functions
%%
%%%=========================================================================
%%% 解包函数
%%%=========================================================================

%% -----------------------------------------------------------------
%% 38000 总成就获取
%% -----------------------------------------------------------------
read(38000, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 38001 获取最近完成成就
%% -----------------------------------------------------------------
read(38001, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 38002 奇珍异宝 列表
%% -----------------------------------------------------------------
read(38002, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 38003 奇珍异宝  领取奖励
%% -----------------------------------------------------------------
read(38003, <<Type:8, AchNum:32>>) ->
	{ok, [Type, AchNum]};

%% -----------------------------------------------------------------
%% 38004 八神珠 已装备
%% -----------------------------------------------------------------
read(38004, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 38005 八神珠 未装备
%% -----------------------------------------------------------------
read(38005, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 38006 八神珠  装备和卸载
%% -----------------------------------------------------------------
read(38006, <<GoodsId:32, Type:8>>) ->
	{ok, [GoodsId, Type]};

%% -----------------------------------------------------------------
%% 38008 人物属性面板称号列表获取
%% -----------------------------------------------------------------
read(38008, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 38009 使用称号
%% -----------------------------------------------------------------
read(38009, <<AchNum:32>>) ->
	{ok, [AchNum]};

%% -----------------------------------------------------------------
%% 38010 取消称号
%% -----------------------------------------------------------------
read(38010, <<AchNum:32>>) ->
	{ok, [AchNum]};

%% -----------------------------------------------------------------
%% 38011 获取称号
%% -----------------------------------------------------------------
read(38011, <<AchNum:32>>) ->
	{ok, [AchNum]};

%% -----------------------------------------------------------------
%% 38012 获取活跃度
%% -----------------------------------------------------------------
read(38012, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 38013 活跃度领取奖品
%% -----------------------------------------------------------------
read(38013, <<Num:8>>) ->
	{ok, [Num]};

%% -----------------------------------------------------------------
%% 38014 新手成就 列表
%% -----------------------------------------------------------------
read(38014, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 错误处理
%% -----------------------------------------------------------------
read(_Cmd, _R) ->
    {error, no_match}.


%%%=========================================================================
%%% 组包函数
%%%=========================================================================
%% -----------------------------------------------------------------
%% 38000 总成就获取
%% -----------------------------------------------------------------
write(38000, [Result]) ->
	{Task, Epic, Trials, Yg, Fs, Interact} = Result,
	{LT, TaskBin} = pack_info_38000(0, [], Task),
	{LE, EpicBin} = pack_info_38000(0, [], Epic),
	{LTR, TrialsBin} = pack_info_38000(0, [], Trials),
	{LYG, YgBin} = pack_info_38000(0, [], Yg),
	{LFS, FsBin} = pack_info_38000(0, [], Fs),
	{LIN, InteractBin} = pack_info_38000(0, [], Interact),
	{ok, pt:pack(38000, <<LT:16, TaskBin/binary,
						  LE:16, EpicBin/binary,
						  LTR:16, TrialsBin/binary,
						  LYG:16, YgBin/binary,
						  LFS:16, FsBin/binary,
						  LIN:16, InteractBin/binary>>)};
	

%% -----------------------------------------------------------------
%% 38001 获取最近完成成就
%% -----------------------------------------------------------------
write(38001, [Result]) ->
	Len = length(Result),
	BinDataList = hendle_38001(Result),
	BinData38001 = tool:to_binary(BinDataList),
	{ok, pt:pack(38001, <<Len:16, BinData38001/binary>>)};

%% -----------------------------------------------------------------
%% 38002 奇珍异宝 列表
%% -----------------------------------------------------------------
write(38002, [Result]) ->
	Len = length(Result),
	BinData38002 = handle_38002([], Result),
	{ok, pt:pack(38002, <<Len:16, BinData38002/binary>>)};
	
%% -----------------------------------------------------------------
%% 38003 奇珍异宝  领取奖励
%% -----------------------------------------------------------------
write(38003, [Type, Result]) ->
	{ok, pt:pack(38003, <<Type:8, Result:16>>)};

%% -----------------------------------------------------------------
%% 38004 八神珠 已装备
%% -----------------------------------------------------------------
write(38004, [Result]) ->
	{Len, BinData38004} = handle_38004(0, [], Result), 
	{ok, pt:pack(38004, <<Len:16, BinData38004/binary>>)};

%% -----------------------------------------------------------------
%% 38005 八神珠 未装备
%% -----------------------------------------------------------------
write(38005, [Result]) ->
	{Len, BinData38005} = handle_38005(0, [], Result),
	{ok, pt:pack(38005, <<Len:16, BinData38005/binary>>)};

%% -----------------------------------------------------------------
%% 38006 八神珠  装备和卸载
%% -----------------------------------------------------------------
write(38006, [Result]) ->
	{Ret, Type, GoodsId, GoodsTypeId, Cell} = Result,
	{ok, pt:pack(38006, <<Ret:8, Type:8, GoodsId:32, GoodsTypeId:32, Cell:8>>)};

%% -----------------------------------------------------------------
%% 38007 通知某个成就完成
%% -----------------------------------------------------------------
write(38007, [AchNum]) ->
	{ok, pt:pack(38007, <<AchNum:32>>)};

%% -----------------------------------------------------------------
%% 38008 人物属性面板称号列表获取
%% -----------------------------------------------------------------
write(38008, [Result]) ->
	{AchTs, ComTs, SpecTs} = Result,
	{ALen, AchTsBin} = handle_38008(0, [], AchTs),
	{CLen, ComTsBin} = handle_38008(0, [], ComTs),
	{SLen, SpecTsBin} = handle_38008(0, [], SpecTs),
	{ok, pt:pack(38008, <<ALen:16, AchTsBin/binary, CLen:16, ComTsBin/binary, SLen:16, SpecTsBin/binary>>)};
	
	
%% -----------------------------------------------------------------
%% 38009 使用称号
%% -----------------------------------------------------------------
write(38009, [Result, AchNum]) ->
	{ok, pt:pack(38009, <<Result:16, AchNum:32>>)};

%% -----------------------------------------------------------------
%% 38010 取消称号
%% -----------------------------------------------------------------
write(38010, [Result, AchNum]) ->
	{ok, pt:pack(38010, <<Result:16, AchNum:32>>)};

%% -----------------------------------------------------------------
%% 38011 获取称号
%% -----------------------------------------------------------------
write(38011, [Result, AchNum]) ->
	{ok, pt:pack(38011, <<Result:16, AchNum:32>>)};


%% -----------------------------------------------------------------
%% 38012 获取活跃度
%% -----------------------------------------------------------------
write(38012, [Result]) ->
	{Act, Actions, Goods} = Result,
	ALen = length(Actions),
	GLen = length(Goods),
	AList = lists:map(fun(A) -> <<A:8>> end, Actions),
	GList = lists:map(fun(G) -> <<G:8>> end, Goods),
	AListBin = tool:to_binary(AList),
	GListBin = tool:to_binary(GList),
%% 	?DEBUG("Act:~p, ALen:~p, Actions:~p, GLen:~p, Goods:~p", [Act, ALen, Actions, GLen, Goods]),
	{ok, pt:pack(38012, <<Act:16, ALen:16, AListBin/binary, GLen:16, GListBin/binary>>)};
	
%% -----------------------------------------------------------------
%% 38013 活跃度领取奖品
%% -----------------------------------------------------------------
write(38013, [Result]) ->
	{ok, pt:pack(38013, <<Result:8>>)};

%% -----------------------------------------------------------------
%% 38014 新手成就 列表
%% -----------------------------------------------------------------
write(38014, [Result]) ->
Len = length(Result),
	BinData38002 = handle_38002([], Result),%%与38002协议数据相同 ，直接使用相同方法处理
	{ok, pt:pack(38014, <<Len:16, BinData38002/binary>>)};


%% -----------------------------------------------------------------
%% 38015 通知称号被取消
%% -----------------------------------------------------------------
write(38015, [Tid]) ->
	{ok, pt:pack(38015, <<Tid:32>>)};

%% -----------------------------------------------------------------
%% 38016 通知奖励领取
%% -----------------------------------------------------------------
write(38016, [Result]) ->
	List = lists:map(fun(Elem) -> 
					  <<Elem:32>>
			  end, Result),
	Len = length(List),
	ListBin = tool:to_binary(List),
%% 	?DEBUG("Result:~p", [Result]),
	{ok, pt:pack(38016, <<Len:16, ListBin/binary>>)};

%% -----------------------------------------------------------------
%% 错误处理
%% -----------------------------------------------------------------
write(Cmd, _R) ->
	?INFO_MSG("~s_errorcmd_[~p] ",[misc:time_format(yg_timer:now()), Cmd]),
    {ok, pt:pack(0, <<>>)}.
%%
%% Local Functions
%%
hendle_38001(Result) ->
	lists:map(fun(Elem) ->
					  Num = Elem#ets_log_ach_f.ach_num,
					  <<Num:32>>
			  end, Result).

pack_info_38000(Num, Result, []) ->
	{Num, tool:to_binary(Result)};
pack_info_38000(Num, NewList, [Info|List]) ->
	{AchNum, Count, Total, Ach} = Info,
	case Count >= Total of
		false  ->
			case Ach >= 1 of
				true ->
					Result = [<<AchNum:32, Total:32, Total:32, 1:8>>|NewList];
				false ->
					Result = [<<AchNum:32, Count:32, Total:32, Ach:8>>|NewList]
			end;
		true ->
			case Ach >= 1 of
				true ->
					Result = [<<AchNum:32, Total:32, Total:32, 1:8>>|NewList];
				false ->
					Result = [<<AchNum:32, Total:32, Total:32, Ach:8>>|NewList]
			end
	end,
	pack_info_38000(Num+1, Result, List).

handle_38002(List, []) ->
	tool:to_binary(List);
handle_38002(List, [Elem|Rest]) ->
	{AchNum, IsFinished, IsGot} = Elem,
	Result = [<<AchNum:32, IsFinished:8, IsGot:8>>|List],
	handle_38002(Result, Rest).
	
handle_38004(Num, List, []) ->
	{Num, tool:to_binary(List)};
handle_38004(Num, List, [Elem|Rest]) ->
	{GoodsId, GoodsTypeId, Cell} = Elem,
	Result = [<<GoodsId:32, GoodsTypeId:32, Cell:8>>|List],
	handle_38004(Num+1, Result, Rest).

handle_38005(Num, List, []) ->
	{Num, tool:to_binary(List)};
handle_38005(Num, List, [Elem|Rest]) ->
	{GoodsId, GoodsTypeId} = Elem,
	Result = [<<GoodsId:32, GoodsTypeId:32>>|List],
	handle_38005(Num+1, Result, Rest).

handle_38008(Num, List, []) ->
	{Num, tool:to_binary(List)};
handle_38008(Num, List, [Elem|Rest]) ->
	{A, B, C, D} = Elem,
	Result = [<<A:32,B:32,C:8, D:8>>|List],
	handle_38008(Num+1, Result, Rest).